﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using System.Xml;
using SdmxMl.Common;
using SdmxMl.Helper;
using SdmxMl.Manager;

namespace SdmxMl.Structure
{
    /// <summary>
    /// CategoryIDType describes a structure which can provide a path inside a
    /// hierarchical category scheme. Each node (category) of the referenced scheme
    /// is represented by a CategoryID element, with sub-categories represented by
    /// the child CategoryID element. Each CategoryID element must be given a node
    /// identifier in the ID field, which corresponds to the ID of the category.
    /// It is not necessary to represent the full category path with the nesting
    /// structure if each node within the hierarchical category scheme has a unique id.
    /// </summary>
    public class CategoryIDType
    {
        #region Properties

        private string _id;
        public string ID
        {
            get { return _id; }
            set { _id = value; }
        }

        private string _version;
        public virtual string Version
        {
            get { return _version; }
            set
            {
                _version = value;
                if (string.IsNullOrEmpty(_version))
                    _version = "1.0";
            }
        }

        private CategoryIDTypeList _categoryIDList;
        public CategoryIDTypeList CategoryIDList
        {
            get { return _categoryIDList; }
        }

        #endregion //Properties

        #region Constructors

        public CategoryIDType()
        {
            Init();
        }

        private void Init()
        {
            _id = "undefined";
            _categoryIDList = new CategoryIDTypeList();
        }

        public CategoryIDType(XmlNode nodeArtefact, XmlNamespaceManager nsmgr)
        {
            Init();

            _id = Xmlhelper.GetNodeText(nodeArtefact, "./structure:ID", nsmgr);
            Version = Xmlhelper.GetNodeText(nodeArtefact, "./structure:CategoryVersion", nsmgr);

            // Reported Categories
            XmlNodeList nodeList = nodeArtefact.SelectNodes("./structure:CategoryID", nsmgr);
            foreach (XmlNode nd in nodeList)
            {
                CategoryIDType cat = new CategoryIDType(nd, nsmgr);
                _categoryIDList.Add(cat);
            }
        }

        #endregion //Constructors

        #region XML Representation

        public void WriteXml(XmlTextWriter xw, string prefix, string tag)
        {
            xw.WriteStartElement(prefix, tag, null);

            // Required
            xw.WriteElementString(prefix, "ID", null, _id);
            Xmlhelper.WriteElementIfExist(xw, prefix, "CategoryVersion", Version);

            // Write child categories
            foreach (CategoryIDType cat in _categoryIDList)
                cat.WriteXml(xw, prefix, "CategoryID");

            xw.WriteEndElement();
        }

        #endregion //XMl Representation

    }

    public class CategoryIDTypeList : List<CategoryIDType>
    {
    }
}
